﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace QuickStart
{
	public class EditCustomersPresenter
	{
		IEditCustomersView view;
		ICustomerRepository repository;

		public EditCustomersPresenter(IEditCustomersView view, ICustomerRepository repository)
		{
			this.view = view;
			this.repository = repository;
			view.Presenter = this;
		}

		public void Search(string criteria)
		{
			if (!String.IsNullOrEmpty(criteria))
			{
				view.SetResults(repository.FindByName(criteria));
			}
			else
			{
				view.SetResults(repository.GetAll());
			}
		}

		public void Save(Customer customer)
		{
			if (repository.Update(customer))
				view.SetStatus(String.Format(
					CultureInfo.CurrentCulture,
					Properties.Resources.SavedCustomer,
					customer.FirstName, customer.LastName));
			else
				view.SetStatus(String.Format(
					CultureInfo.CurrentCulture,
					Properties.Resources.SaveFailed,
					customer.FirstName, customer.LastName));
		}
	}
}
